$(document).ready(onDocumentReady);
var previousAdressPath;
var previousAdressPathNames = [];

function onDocumentReady(){		
	$('#homeTab').hide();
	$('#shopListTab').hide();
	$('#historyTab').hide();
	$('#profileTab').hide();
	$('#helpTab').hide();

	$('#formsContainer').hide();
	$('#loginFormContainer').hide();
	$('#registerFormContainer').hide();
	$('#homeLoginInfo').hide();
	$('#editTabContent').hide();
	$('#printTabContent').hide();
	
	// adress is init after login
	//$.address.init(addressInit);
	//$.address.change(addressChange);
}

function addressInit(event) {	
	//alert("init: " + $.address.path() + " : " + loggedInUser);
	
	if(loggedInUser == null || $.address.pathNames()[0] == null){	
		if($.address.pathNames()[0] != "home" && ($.address.pathNames()[1] != "register" || $.address.pathNames()[1] != "login")){
			$.address.value("/home/login");				
		}
	}
	
	if($.address.pathNames()[0] == "home"){
		$('#homeTab').show();
		if($.address.pathNames()[1] == "login"){
			if(loggedInUser == null){				
				$('#formsContainer').show();				
				$('#loginFormContainer').show();				
			}else{
				$('#homeLoginInfo').show();				
			}
		}		
		if($.address.pathNames()[1] == "register"){
			$('#formsContainer').show();
			$('#registerFormContainer').show();
		}		
	}

	if($.address.pathNames()[0] == "shoppinglist"){
		$('#shopListTab').show();
		
		if($.address.pathNames()[1] == "edit"){		
			showEditTab();
		}			
		if($.address.pathNames()[1] == "print"){
			showPrintTab();
		}
	}
	
	previousAdressPathNames = $.address.pathNames();
	previousAdressPath = $.address.path();
}

function addressChange() {
	/*$.address.value()
	$.address.path()
	$.address.pathNames()
	$.address.parameterNames()
	$.address.queryString()*/
	
	//alert("change: " + $.address.path() + " : " + loggedInUser);
	
	if(loggedInUser == null){
		$('#headerLoginInfoRepeater').fadeOut();
		if($.address.pathNames()[0] != "home"){
			$.address.value("/home/login");
		}
	}
	
	if($.address.pathNames()[0] == null){
		$.address.value("/home/login");	
	}else if($.address.pathNames()[0] == "logout"){
		logout();
		$.address.value("/home/login");
	}else {
		if(previousAdressPathNames[0] != $.address.pathNames()[0]){
			//if the adress for level 0 changed
			$('#navHome').removeClass("selected");		
			$('#navShoppingList').removeClass("selected");		
			$('#navHistory').removeClass("selected");		
			$('#navProfile').removeClass("selected");		
			$('#navHelp').removeClass("selected");		
			
			$('#homeTab').hide();
			$('#shopListTab').hide();
			$('#historyTab').hide();
			$('#profileTab').hide();
			$('#helpTab').hide();
		}
		
		if(previousAdressPathNames[1] != $.address.pathNames()[1]){
			//if the adress for level 1 changed
			$('#loginFormContainer').slideUp();
			$('#registerFormContainer').slideUp();			
			$('#homeLoginInfo').slideUp();
		}
		
		if($.address.pathNames()[0] == "home"){
			$('#homeTab').slideDown();
			$('#navHome').addClass("selected");	
						
			if($.address.pathNames()[1] == null){		
				$.address.value("/home/info");
			}			
			if($.address.pathNames()[1] == "login"){
				if(loggedInUser == null){					
					$('#formsContainer').slideDown();				
					$('#loginFormContainer').slideDown();				
					$('#homeLoginInfo').slideUp();				
				}else{
					$('#formsContainer').slideUp();				
					$('#homeLoginInfo').slideDown();				
				}
			}			
			if($.address.pathNames()[1] == "register"){
				$('#formsContainer').slideDown();				
				$('#registerFormContainer').slideDown();
			}
		}
		
		if($.address.pathNames()[0] == "shoppinglist"){
			$('#shopListTab').slideDown();		
			$('#navShoppingList').addClass("selected");	
			
			if($.address.pathNames()[1] == "edit"){		
				showEditTab();
			}			
			if($.address.pathNames()[1] == "print"){
				showPrintTab();
			}
		}
		
		if($.address.pathNames()[0] == "history"){
			$('#historyTab').slideDown();
			$('#navHistory').addClass("selected");					
		}
			
		if($.address.pathNames()[0] == "profile"){
			$('#profileTab').slideDown();
			$('#navProfile').addClass("selected");			
		}
		
		if($.address.pathNames()[0] == "help"){
			$('#helpTab').slideDown();		
			$('#navHelp').addClass("selected");		
		}
		
		previousAdressPathNames = $.address.pathNames();
		previousAdressPath = $.address.path();
	}
}

function showEditTab(){
	$('#ordersContainer').addClass("grid_6");
	$('#ordersContainer').removeClass("alpha");
	$('#ordersContainer').removeClass("grid_9");
	$('#categoriesContainer').css("display", "block");
	
	// set the correct tab
	$('#btnEdit').addClass("inactiveTabButton");
	$('#btnEdit').removeClass("activeTabButton");		
	$('#btnPrint').addClass("activeTabButton");
	$('#btnPrint').removeClass("inactiveTabButton");

	$('#printTabContent').hide();
	$('#editTabContent').show();
}

function showPrintTab(){
	$('#ordersContainer').removeClass("grid_6");
	$('#ordersContainer').addClass("grid_9");
	$('#ordersContainer').addClass("alpha");
	$('#categoriesContainer').css("display", "none");
	
	// set the correct tab
	$('#btnEdit').addClass("activeTabButton");
	$('#btnEdit').removeClass("inactiveTabButton");		
	$('#btnPrint').addClass("inactiveTabButton");
	$('#btnPrint').removeClass("activeTabButton");
	
	$('#editTabContent').hide();
	$('#printTabContent').show();
}